package com.ren.newvideoplayer.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ren.newvideoplayer.R;
import com.ren.newvideoplayer.adapter.VideoPlayerRecyclerAdapter;
import com.ren.newvideoplayer.module.Video;
import com.ren.newvideoplayer.player.NiceVideoPlayer;
import com.ren.newvideoplayer.player.NiceVideoPlayerManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private List<Video> mDataList = new ArrayList<>();
    private VideoPlayerRecyclerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        if(mAdapter == null){
            mAdapter = new VideoPlayerRecyclerAdapter(this,mDataList);
            recyclerView.setAdapter(mAdapter);
        }else{
            mAdapter.notifyDataSetChanged();
        }
        initListener();
    }

    private void initListener() {
        recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                NiceVideoPlayer niceVideoPlayer = (NiceVideoPlayer) view.findViewById(R.id.nice_video_player);
                if (niceVideoPlayer != null) {
                    niceVideoPlayer.release();
                }
            }
        });
    }

    private void initData() {
        for(int i = 0;i < 10;i++){
            Video bean = new Video("办公室小野开番外了，居然在办公室开澡堂！老板还点赞？",
                    "http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-30-43.jpg",
                    "http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-33-30.mp4");
            mDataList.add(bean);
        }
    }
    @Override
    public void onBackPressed() {
        if (NiceVideoPlayerManager.instance().onBackPressd()) {
            return;
        }
        super.onBackPressed();
    }
}